import { configureStore } from '@reduxjs/toolkit'
import {
  useSelector,
  TypedUseSelectorHook,
  useDispatch,
  shallowEqual
} from 'react-redux'

import userSlice from './modules/user'
import recommendSlice from './modules/recommend'

const store = configureStore({
  reducer: {
    user: userSlice,
    recommend: recommendSlice
  }
})

// 拿到store中所有模块下initialState参数的类型
type GetStatFnType = typeof store.getState
// 此时GetStatFnType是个函数类型 使用ReturnType 拿到类型
type IRootState = ReturnType<GetStatFnType>
type DispatchType = typeof store.dispatch
// useAppSelector: 统一给useSelector的state设置类型
export const useAppSelector: TypedUseSelectorHook<IRootState> = useSelector
export const useAppDispatch: () => DispatchType = useDispatch
export const appShallowEqual = shallowEqual

export default store
